package com.zhl.config;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

public class DisplayNameCondition implements Condition {

    private AnnotationConfigApplicationContext context;

    public DisplayNameCondition(AnnotationConfigApplicationContext context) {
        this.context = context;
    }

    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        // 获取 ApplicationContext 的 displayName
//        String displayName = context.getApplicationContext().getDisplayName();
//        // 判断 displayName 是否符合预期
//        return "myAppContext".equals(displayName);

        Object displayNameObj = context.getBeanFactory().getBeanDefinition(context.getBeanFactory().getBeanNamesForType(AnnotationConfigApplicationContext.class)[0]).getAttribute("displayName");
        if (displayNameObj != null) {
            String displayName = displayNameObj.toString();
            // 判断 displayName 是否符合预期
            return "myAppContext".equals(displayName);
        }
        return false;
    }
}
